package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	/**
	 * 查询所有分类(1、2、3级分类)
	 * @return
	 */
	@Override
	public List<JSONObject> getBaseCategoryList() {
		List<JSONObject> allCategroyList = new ArrayList<>();
		// 1. 查询分类视图中所有的数据
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		if (!CollectionUtil.isEmpty(baseCategoryViewList)) {
			// 2. 获取一级分类列表
			// key: category1Id, value: 根据一级分类id组成的二级分类列表
			Map<Long, List<BaseCategoryView>> categroy1Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
			for (Map.Entry<Long, List<BaseCategoryView>> entry1 : categroy1Map.entrySet()) {
				Long category1Id = entry1.getKey();
				String category1Name = entry1.getValue().get(0).getCategory1Name();
				JSONObject category1 = new JSONObject();
				category1.put("categoryId", category1Id);
				category1.put("categoryName", category1Name);
				// 3. 处理二级分类
				Map<Long, List<BaseCategoryView>> category2Map = entry1.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
				List<JSONObject> category2List = new ArrayList<>();
				for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
					Long category2Id = entry2.getKey();
					String category2Name = entry2.getValue().get(0).getCategory2Name();
					JSONObject category2 = new JSONObject();
					category2.put("categoryId", category2Id);
					category2.put("categoryName", category2Name);
					// 4. 处理三级分类
					List<BaseCategoryView> category3List = entry2.getValue();
					for (BaseCategoryView view : category3List) {
						JSONObject category3 = new JSONObject();
						category3.put("categoryId", view.getCategory3Id());
						category3.put("categoryName", view.getCategory3Name());
					}
					category2.put("categroyChild", category3List);
					category2List.add(category2);
				}
				category1.put("categoryChild", category2List);
				allCategroyList.add(category1);
			}
		}

		return allCategroyList;
	}

	/**
	 * 根据一级分类Id获取分类属性以及属性值（标签名，标签值）列表
	 * @param category1Id 一级分类id
	 * @return
	 */
	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		//方式一：采用QueryWrapper实现  1、根据一级分类ID查询属性列表  2、遍历属性列表，根据属性ID查询属性值表
		//方式二：采用动态SQL在持久层完成封装一对多
		return baseAttributeMapper.findAttribute(category1Id);
	}


}
